﻿using System.Text;
using PPI.Data;
using PPI.Entities;
using System;
using System.Data.SqlClient;
using System.Collections.Generic;
using PPI.Entities.Interface;
using PPI.Entities.Lookup;
using System.Data;
using System.IO;
using PPI.Entities.Utility;
using PPI.Mobile;
using PPI.Mobile.Logic;
using VMind.Global;
using DBHelper=PPI.Data.DBHelper;

namespace PPI.ServiceLogic
{
    public class LVendor_Visit_XMissionStructure :IMobileTable
    {
        #region CRUD

        public static int Save(Int32 id, Int32 projectId, Int32 utilityId, Int32 visitTypeId, Int32 topTypeId, double latitude, double longitude, double altitude, Int32 satelliteCount, DateTime satelliteTime, bool solutionAvailable, Int32 handheldId, Int32 contactId, DateTime date, int totalImages, string connectionKey)
        {
            Int32 returnId = id;
            if (id == Int32.MinValue)
                returnId = Insert(projectId, utilityId, visitTypeId, topTypeId, latitude, longitude, altitude, satelliteCount, satelliteTime, solutionAvailable, handheldId, contactId, date, totalImages, connectionKey);
            else
                Update(id,projectId,utilityId,visitTypeId,topTypeId,latitude,longitude,altitude,satelliteCount,satelliteTime,solutionAvailable,handheldId,contactId,date,totalImages,connectionKey);
            return returnId;
        }

        private static void Update(Int32 id, Int32 projectId, Int32 utilityId, Int32 visitTypeId, Int32 topTypeId, double latitude, double longitude, double altitude, Int32 satelliteCount, DateTime satelliteTime, bool solutionAvailable, Int32 handheldId, Int32 contactId, DateTime date, int totalImages, string connectionKey)
        {
            DVendor_Visit_XMissionStructure.Update(id,projectId,utilityId,visitTypeId,topTypeId,latitude,longitude,altitude,satelliteCount,satelliteTime,solutionAvailable,handheldId,contactId,date,totalImages,connectionKey);
        }

        private static Int32 Insert(Int32 projectId, Int32 utilityId, Int32 visitTypeId, Int32 topTypeId, double latitude, double longitude, double altitude, Int32 satelliteCount, DateTime satelliteTime, bool solutionAvailable, Int32 handheldId, Int32 contactId, DateTime date, int totalImages, string connectionKey)
        {
            return
                DVendor_Visit_XMissionStructure.Insert(projectId, utilityId, visitTypeId, topTypeId, latitude, longitude,
                                                       altitude, satelliteCount, satelliteTime, solutionAvailable,
                                                       handheldId, contactId, date, totalImages, connectionKey);

        }

        public static void Delete(Int32 id, string connectionkey)
        {
            DVendor_Visit_XMissionStructure.Delete(id, connectionkey);
        }

        #endregion

        private List<EVendor_Visit_XMissionStructure> LoadVisits(string connectionKey)
        {
            DataTable _Datatable = DVendor_Visit_XMissionStructure.GetServerVisits(connectionKey).Tables[0];
            return ParseDataTable(_Datatable, connectionKey);

        }

        private List<EVendor_Visit_XMissionStructure> ParseDataTable(DataTable _Datatable, string connectionKey)
        {
            Int32 StructureIdFlag = Int32.MinValue;
            Int32 _CurrentStructureID;

            EVendor_Visit_XMissionStructure _Structure;
            List<EVendor_Visit_XMissionStructure> _StructureList = new List<EVendor_Visit_XMissionStructure>();

            EVendor_Visit_XMissionStructures_Pole _Pole;
            List<EVendor_Visit_XMissionStructures_Pole> _PoleList = new List<EVendor_Visit_XMissionStructures_Pole>();

            List<EVendor_Visit_XMissionStructures_Image> _ImageList = LVendor_Visit_XMissionStructures_Image.GetServerImages(connectionKey);


            DataRow _DataRow = null;
            DataRow _PreviusRow = null;
            for (int i = 0; i < _Datatable.Rows.Count; i++)
            {
                _DataRow = _Datatable.Rows[i];
                if (i != 0)
                    _PreviusRow = _Datatable.Rows[i - 1];

                _CurrentStructureID = ConvertHelper.ToInt32(_DataRow["Vendor_Visit_XMissionStructureId"]);


                if (StructureIdFlag != Int32.MinValue)
                {
                    //Do control break
                    if (StructureIdFlag != _CurrentStructureID)
                    {
                        _Structure = new EVendor_Visit_XMissionStructure(ConvertHelper.ToInt32(_PreviusRow["Vendor_Visit_XMissionStructureId"]));
                        _Structure.UtilityAsset = new EUtility_Asset_XMissionStructure(ConvertHelper.ToInt32(_PreviusRow["Utility_Asset_XMissionStructureFK"]));
                        _Structure.Altitude = ConvertHelper.Todouble(_PreviusRow["Structure_Altitude"]);
                        _Structure.Longitude = ConvertHelper.Todouble(_PreviusRow["Structure_Longitude"]);
                        _Structure.Latitude = ConvertHelper.Todouble(_PreviusRow["Structure_Latitude"]);
                        _Structure.SatelliteCount = ConvertHelper.Toint(_PreviusRow["Structure_SatelliteCount"]);
                        _Structure.Date = ConvertHelper.ToDateTime(_PreviusRow["Structure_SatelliteTime"]);
                        _Structure.SatelliteTime = ConvertHelper.ToDateTime(_PreviusRow["Structure_SatelliteTime"]);
                        _Structure.SolutionAvailable = ConvertHelper.Tobool(_PreviusRow["Structure_SolutionAvailable"]);
                        foreach (EBasePole p in _PoleList)
                        {
                            if (!_Structure.Poles.Contains(p))
                            {
                                p.Asset = _Structure;
                                _Structure.Poles.Add(p);
                            }
                        }

                        _Structure.Vendor_Visit_Type = new EVendor_Visit_Type(ConvertHelper.Toint(_PreviusRow["Vendor_Visit_TypeFK"]), ConvertHelper.Tostring(_PreviusRow["visit_Name"]));
                        _Structure.Vendor_Project = new EVendor_Project(ConvertHelper.Toint(_PreviusRow["Vendor_ProjectFK"]), ConvertHelper.Tostring(_PreviusRow["vendor_Name"]));
                        _Structure.Vendor_Project.Vendor = new EVendor(ConvertHelper.Toint(_PreviusRow["Vendor_Id"]), ConvertHelper.Tostring(_PreviusRow["Company_Name"]), "");
                        _Structure.Vendor_Contact = new EVendor_Contact(ConvertHelper.ToInt32(_PreviusRow["vendor_Contact_Id"]));
                        _Structure.Vendor_Contact.Name = ConvertHelper.Tostring(_PreviusRow["Vendor_Contact_Name"]);

                        _Structure.Handheld.Id = ConvertHelper.ToInt32(_PreviusRow["HandheldFk"]);
                        _Structure.TotalImages = ConvertHelper.Toint(_PreviusRow["TotalImages"]);
                        //_Structure.UploadedImages = ConvertHelper.Toint(_PreviusRow["UploadedImages"]);
                        LVendor_Visit_XMissionStructures_Image.AssignImagesToStructure(_Structure, _ImageList);
                        //foreach (EVendor_Visit_XMissionStructures_Image image in _Structure.Images)
                        //{
                        //    if (!image.f.Exists)
                        //    {
                        //        _Structure.UploadedImages--;
                        //    }
                        //}
                        _Structure.TopType = new EXMissionStructures_TopType(ConvertHelper.Toint(_PreviusRow["XMissionStructures_TopTypeFk"]), ConvertHelper.Tostring(_PreviusRow["TopType_Name"]));
                        _StructureList.Add(_Structure);
                        StructureIdFlag = _CurrentStructureID;

                        _PoleList = new List<EVendor_Visit_XMissionStructures_Pole>();
                    }
                }

                // Fill Poles Entities
                if (ConvertHelper.ToInt32(_DataRow["Pole_ID"]) != Int32.MinValue)
                {
                    _Pole = new EVendor_Visit_XMissionStructures_Pole();
                    _Pole.Altitude = ConvertHelper.Todouble(_DataRow["Pole_Altitude"]);
                    _Pole.Latitude = ConvertHelper.Todouble(_DataRow["Pole_Latitude"]);
                    _Pole.Longitude = ConvertHelper.Todouble(_DataRow["Pole_Longitude"]);
                    _Pole.Id = ConvertHelper.ToInt32(_DataRow["Pole_ID"]);
                    _Pole.SatelliteTime = ConvertHelper.ToDateTime(_DataRow["Pole_SatelliteTime"]);
                    _Pole.SatelliteCount = ConvertHelper.Toint(_DataRow["Pole_SatelliteCount"]);
                    _Pole.SolutionAvailable = ConvertHelper.Tobool(_DataRow["Pole_SolutionAvailable"]);
                    _Pole.Asset = new EVendor_Visit_XMissionStructure(ConvertHelper.ToInt32(_DataRow["Vendor_Visit_XMissionStructureId"]));
                    LVendor_Visit_XMissionStructures_Image.AssignImagesToPole(_Pole, _ImageList);
                    _Pole.Year = ConvertHelper.Toint(_DataRow["Pole_Year"]);

                    _Pole.AID = ConvertHelper.Tostring(_DataRow["Pole_AID"]);
                    _Pole.LVisit = ConvertHelper.Toint(_DataRow["Pole_LVisit"]);
                    _Pole.Number = ConvertHelper.Tostring(_DataRow["Pole_Number"]);
                    _Pole.Length = ConvertHelper.Todouble(_DataRow["Pole_Length"]);
                    _Pole.LVisitor = ConvertHelper.Tostring(_DataRow["Pole_LVisitor"]);


                    _Pole.Class = new EXMissionStructures_PoleClass(ConvertHelper.Toint(_DataRow["Pole_ClassID"]), ConvertHelper.Tostring(_DataRow["Classes_Name"]));
                    _Pole.TopType = new EXMissionStructures_PoleTopType(ConvertHelper.Toint(_DataRow["Pole_TopID"]), ConvertHelper.Tostring(_DataRow["Top_Name"]));
                    _Pole.Species = new EXMissionStructures_PoleSpecies(ConvertHelper.Toint(_DataRow["Pole_SpeciesID"]), ConvertHelper.Tostring(_DataRow["Species_Name"]));
                    _Pole.Treatment = new EXMissionStructures_PoleTreatment(ConvertHelper.Toint(_DataRow["Pole_TreatmentID"]), ConvertHelper.Tostring(_DataRow["Treatment_Name"]));


                    _PoleList.Add(_Pole);
                }


                if (StructureIdFlag == Int32.MinValue)
                    StructureIdFlag = _CurrentStructureID;


            }
            if (_Datatable.Rows.Count > 0)
            {
                _PreviusRow = _Datatable.Rows[_Datatable.Rows.Count - 1];
                //Add last structure
                _Structure = new EVendor_Visit_XMissionStructure(ConvertHelper.ToInt32(_PreviusRow["Vendor_Visit_XMissionStructureId"]));
                _Structure.UtilityAsset = new EUtility_Asset_XMissionStructure(ConvertHelper.ToInt32(_PreviusRow["Utility_Asset_XMissionStructureFK"]));
                _Structure.Altitude = ConvertHelper.Todouble(_PreviusRow["Structure_Altitude"]);
                _Structure.Longitude = ConvertHelper.Todouble(_PreviusRow["Structure_Longitude"]);
                _Structure.Latitude = ConvertHelper.Todouble(_PreviusRow["Structure_Latitude"]);
                _Structure.SatelliteCount = ConvertHelper.Toint(_PreviusRow["Structure_SatelliteCount"]);
                _Structure.SatelliteTime = ConvertHelper.ToDateTime(_PreviusRow["Structure_SatelliteTime"]);
                _Structure.SolutionAvailable = ConvertHelper.Tobool(_PreviusRow["Structure_SolutionAvailable"]);
                _Structure.Date = ConvertHelper.ToDateTime(_PreviusRow["Structure_SatelliteTime"]);
                _Structure.Vendor_Visit_Type = new EVendor_Visit_Type(ConvertHelper.Toint(_PreviusRow["Vendor_Visit_TypeFK"]), ConvertHelper.Tostring(_PreviusRow["visit_Name"]));
                _Structure.Vendor_Project = new EVendor_Project(ConvertHelper.Toint(_PreviusRow["Vendor_ProjectFK"]), ConvertHelper.Tostring(_PreviusRow["vendor_Name"]));
                _Structure.Vendor_Project.Vendor = new EVendor(ConvertHelper.Toint(_PreviusRow["Vendor_Id"]), ConvertHelper.Tostring(_PreviusRow["Company_Name"]), "");
                _Structure.Vendor_Contact = new EVendor_Contact(ConvertHelper.ToInt32(_PreviusRow["vendor_Contact_Id"]));
                _Structure.Vendor_Contact.Name = ConvertHelper.Tostring(_PreviusRow["Vendor_Contact_Name"]);

                _Structure.Handheld.Id = ConvertHelper.ToInt32(_PreviusRow["HandheldFk"]);
                _Structure.TotalImages = ConvertHelper.Toint(_PreviusRow["TotalImages"]);
                LVendor_Visit_XMissionStructures_Image.AssignImagesToStructure(_Structure, _ImageList);
                _Structure.TopType = new EXMissionStructures_TopType(ConvertHelper.Toint(_PreviusRow["XMissionStructures_TopTypeFk"]), ConvertHelper.Tostring(_PreviusRow["TopType_Name"]));

                foreach (EBasePole p in _PoleList)
                {
                    if (!_Structure.Poles.Contains(p))
                    {
                        p.Asset = _Structure;
                        _Structure.Poles.Add(p);
                    }
                }
                _Structure.Date = ConvertHelper.ToDateTime(_PreviusRow["Structure_SatelliteTime"]);
                _StructureList.Add(_Structure);
            }

            return _StructureList;
        }

        public List<EMobileQuery> CreateInsertMobileQuery(string connectionKey)
        {
            List<EMobileQuery> listQueries = new List<EMobileQuery>();

            EMobileQuery _MobileQueryStructures = new EMobileQuery();
            EMobileQuery _MobileQueryPoles = new EMobileQuery();
            EMobileQuery _MobileQueryImages = new EMobileQuery();

            StringBuilder _QueryStructures = new StringBuilder();
            StringBuilder _QueryPoles = new StringBuilder();
            StringBuilder _QueryImages = new StringBuilder();

            List<SqlParameter> _ParametersStructures = null;
            List<SqlParameter> _ParametersPoles = null;
            List<SqlParameter> _ParametersImages = null;

            listQueries.Add(new EMobileQuery("DELETE tb_Vendor_Visit_Images"));
            listQueries.Add(new EMobileQuery("DELETE tb_Vendor_Visit_XStructureMappingPoles"));
            listQueries.Add(new EMobileQuery("DELETE tb_Vendor_Visits"));

            #region QueryStructures

            _QueryStructures.AppendLine("Insert into tb_Vendor_Visits ");
            _QueryStructures.Append("     ( ");
            _QueryStructures.Append("         VisitId, ");
            _QueryStructures.Append("         VisitTypeFk, ");
            _QueryStructures.Append("         Latitude, ");
            _QueryStructures.Append("         Longitude, ");
            _QueryStructures.Append("         Altitude, ");
            _QueryStructures.Append("         SatelliteCount, ");
            _QueryStructures.Append("         SatelliteTime, ");
            _QueryStructures.Append("         SolutionAvailable, ");
            _QueryStructures.Append("         ProjectFk, ");
            _QueryStructures.Append("         VendorFk, ");
            _QueryStructures.Append("         UserFk, ");
            _QueryStructures.Append("         Handheld, ");
            _QueryStructures.Append("         TopTypeFk, ");
            _QueryStructures.Append("         AssetFk ");
            _QueryStructures.Append("     )Values( ");
            _QueryStructures.Append("         @VisitId,");
            _QueryStructures.Append("         @VisitTypeFk,");
            _QueryStructures.Append("         @Latitude,");
            _QueryStructures.Append("         @Longitude,");
            _QueryStructures.Append("         @Altitude,");
            _QueryStructures.Append("         @SatelliteCount,");
            _QueryStructures.Append("         @SatelliteTime,");
            _QueryStructures.Append("         @SolutionAvailable,");
            _QueryStructures.Append("         @ProjectFk,");
            _QueryStructures.Append("         @VendorFk,");
            _QueryStructures.Append("         @UserFk,");
            _QueryStructures.Append("         @Handheld,");
            _QueryStructures.Append("         @TopTypeFk,");
            _QueryStructures.Append("         @AssetFk");
            _QueryStructures.Append("     )");
            _MobileQueryStructures.Query = _QueryStructures.ToString();


            #endregion

            #region QueryPoles

            _QueryPoles.AppendLine("Insert into tb_Vendor_Visit_XStructureMappingPoles ");
            _QueryPoles.AppendLine(" (");
            _QueryPoles.AppendLine("     PoleId,");
            _QueryPoles.AppendLine("     VisitFk,");
            _QueryPoles.AppendLine("     TopTypeFk,");
            _QueryPoles.AppendLine("     SpeciesFk,");
            _QueryPoles.AppendLine("     TreatmentFk,");
            _QueryPoles.AppendLine("     Year,");
            _QueryPoles.AppendLine("     ClassFk,");
            _QueryPoles.AppendLine("     Latitude,");
            _QueryPoles.AppendLine("     Longitude,");
            _QueryPoles.AppendLine("     Altitude,");
            _QueryPoles.AppendLine("     SatelliteCount,");
            _QueryPoles.AppendLine("     SatelliteTime,");
            _QueryPoles.AppendLine("     SolutionAvailable,");
            _QueryPoles.AppendLine("     AID,");
            _QueryPoles.AppendLine("     LVisit,");
            _QueryPoles.AppendLine("     Number,");
            _QueryPoles.AppendLine("     Length,");
            _QueryPoles.AppendLine("     LVIsitor");
            _QueryPoles.AppendLine(" )Values(");
            _QueryPoles.AppendLine("     @PoleId,");
            _QueryPoles.AppendLine("     @VisitFk,");
            _QueryPoles.AppendLine("     @TopTypeFk,");
            _QueryPoles.AppendLine("     @SpeciesFk,");
            _QueryPoles.AppendLine("     @TreatmentFk,");
            _QueryPoles.AppendLine("     @Year,");
            _QueryPoles.AppendLine("     @ClassFk,");
            _QueryPoles.AppendLine("     @Latitude,");
            _QueryPoles.AppendLine("     @Longitude,");
            _QueryPoles.AppendLine("     @Altitude,");
            _QueryPoles.AppendLine("     @SatelliteCount,");
            _QueryPoles.AppendLine("     @SatelliteTime,");
            _QueryPoles.AppendLine("     @SolutionAvailable,");
            _QueryPoles.AppendLine("     @AID,");
            _QueryPoles.AppendLine("     @LVisit,");
            _QueryPoles.AppendLine("     @Number,");
            _QueryPoles.AppendLine("     @Length,");
            _QueryPoles.AppendLine("     @LVIsitor ");
            _QueryPoles.AppendLine(" )");
            _MobileQueryPoles.Query = _QueryPoles.ToString();


            #endregion

            #region QueryImages
            _QueryImages.AppendLine("Insert into tb_Vendor_Visit_Images ");
            _QueryImages.AppendLine(" (");
            _QueryImages.AppendLine("     ImageId,");
            _QueryImages.AppendLine("     VisitFk,");
            _QueryImages.AppendLine("     PoleFk,");
            _QueryImages.AppendLine("     SolutionAvailable,");
            _QueryImages.AppendLine("     FileName");
            _QueryImages.AppendLine(" )Values(");
            _QueryImages.AppendLine("     @ImageId,");
            _QueryImages.AppendLine("     @VisitFk,");
            _QueryImages.AppendLine("     @PoleFk,");
            _QueryImages.AppendLine("     @SolutionAvailable,");
            _QueryImages.AppendLine("     @FileName");
            _QueryImages.AppendLine(" )");
            _MobileQueryImages.Query = _QueryImages.ToString();


            #endregion
            
            
            
            
            List<EVendor_Visit_XMissionStructure> visits = LoadVisits(connectionKey);

            foreach (EVendor_Visit_XMissionStructure v in visits)
            {

                _ParametersStructures = new List<SqlParameter>();

                #region Parameters
                _ParametersStructures.Add(VMind.Global.DBHelper.CreateIntParameter("@VisitId", ParameterDirection.Input, false, v.Id));
                _ParametersStructures.Add(VMind.Global.DBHelper.CreateIntParameter("@VisitTypeFk", ParameterDirection.Input, false, v.Vendor_Visit_Type.Vendor_Visit_TypeId));
                _ParametersStructures.Add(VMind.Global.DBHelper.CreateFloatParameter("@Latitude", ParameterDirection.Input, false, v.Latitude));
                _ParametersStructures.Add(VMind.Global.DBHelper.CreateFloatParameter("@Longitude", ParameterDirection.Input, false, v.Longitude));
                _ParametersStructures.Add(VMind.Global.DBHelper.CreateFloatParameter("@Altitude", ParameterDirection.Input, false, v.Altitude));
                _ParametersStructures.Add(VMind.Global.DBHelper.CreateIntParameter("@SatelliteCount", ParameterDirection.Input, false, v.SatelliteCount));
                _ParametersStructures.Add(VMind.Global.DBHelper.CreateDateTimeParameter("@SatelliteTime", ParameterDirection.Input, false, v.SatelliteTime));
                _ParametersStructures.Add(VMind.Global.DBHelper.CreateBitParameter("@SolutionAvailable", ParameterDirection.Input, false, v.SolutionAvailable));
                _ParametersStructures.Add(VMind.Global.DBHelper.CreateIntParameter("@UtilityFk", ParameterDirection.Input, true, DBNull.Value));
                _ParametersStructures.Add(VMind.Global.DBHelper.CreateIntParameter("@ProjectFk", ParameterDirection.Input, false, v.Vendor_Project.Id));
                _ParametersStructures.Add(VMind.Global.DBHelper.CreateIntParameter("@VendorFk", ParameterDirection.Input, false, v.Vendor_Project.Vendor.Id));
                _ParametersStructures.Add(VMind.Global.DBHelper.CreateIntParameter("@UserFk", ParameterDirection.Input, false, v.Vendor_Contact.Id));
                _ParametersStructures.Add(VMind.Global.DBHelper.CreateIntParameter("@Handheld", ParameterDirection.Input, true, DBNull.Value));
                _ParametersStructures.Add(VMind.Global.DBHelper.CreateIntParameter("@TopTypeFk", ParameterDirection.Input, false, v.TopType.XMissionStructures_TopTypeId));
                if (v.UtilityAsset.Id != Int32.MinValue)
                    _ParametersStructures.Add(VMind.Global.DBHelper.CreateIntParameter("@AssetFk", ParameterDirection.Input, false, v.UtilityAsset.Id));
                else
                    _ParametersStructures.Add(VMind.Global.DBHelper.CreateIntParameter("@AssetFk", ParameterDirection.Input, true, DBNull.Value));



                #endregion

                _MobileQueryStructures.Parameters.Add(_ParametersStructures);

                foreach (EVendor_Visit_XMissionStructures_Pole p in v.Poles)
                {
                    _ParametersPoles = new List<SqlParameter>();
                    #region Parameters
                    _ParametersPoles.Add(VMind.Global.DBHelper.CreateIntParameter("@PoleId", ParameterDirection.Input, false, p.Id));
                    _ParametersPoles.Add(VMind.Global.DBHelper.CreateIntParameter("@VisitFk", ParameterDirection.Input, false, p.Asset.Id));
                    _ParametersPoles.Add(VMind.Global.DBHelper.CreateIntParameter("@TopTypeFk", ParameterDirection.Input, false, p.TopType.Id));

                    _ParametersPoles.Add(VMind.Global.DBHelper.CreateFloatParameter("@Latitude", ParameterDirection.Input, false, p.Latitude));
                    _ParametersPoles.Add(VMind.Global.DBHelper.CreateFloatParameter("@Longitude", ParameterDirection.Input, false, p.Longitude));
                    _ParametersPoles.Add(VMind.Global.DBHelper.CreateFloatParameter("@Altitude", ParameterDirection.Input, false, p.Altitude));

                    _ParametersPoles.Add(VMind.Global.DBHelper.CreateIntParameter("@SatelliteCount", ParameterDirection.Input, false, p.SatelliteCount));
                    _ParametersPoles.Add(VMind.Global.DBHelper.CreateDateTimeParameter("@SatelliteTime", ParameterDirection.Input, false, p.SatelliteTime));
                    _ParametersPoles.Add(VMind.Global.DBHelper.CreateBitParameter("@SolutionAvailable", ParameterDirection.Input, false, p.SolutionAvailable));

                    #region Treatment
                    if (p.Treatment.Id != Int32.MinValue)
                        _ParametersPoles.Add(VMind.Global.DBHelper.CreateIntParameter("@TreatmentFk", ParameterDirection.Input, false, p.Treatment.Id));
                    else
                        _ParametersPoles.Add(VMind.Global.DBHelper.CreateIntParameter("@TreatmentFk", ParameterDirection.Input, true, DBNull.Value));
                    #endregion

                    #region Specie
                    if (p.Species.Id != Int32.MinValue)
                        _ParametersPoles.Add(VMind.Global.DBHelper.CreateIntParameter("@SpeciesFk", ParameterDirection.Input, false, p.Species.Id));
                    else
                        _ParametersPoles.Add(VMind.Global.DBHelper.CreateIntParameter("@SpeciesFk", ParameterDirection.Input, true, DBNull.Value));
                    #endregion

                    #region Year
                    if (p.Year != Int32.MinValue)
                        _ParametersPoles.Add(VMind.Global.DBHelper.CreateIntParameter("@Year", ParameterDirection.Input, false, p.Year));
                    else
                        _ParametersPoles.Add(VMind.Global.DBHelper.CreateIntParameter("@Year", ParameterDirection.Input, true, DBNull.Value));
                    #endregion

                    #region Class
                    if (p.Class.Id != Int32.MinValue)
                        _ParametersPoles.Add(VMind.Global.DBHelper.CreateIntParameter("@ClassFk", ParameterDirection.Input, false, p.Class.Id));
                    else
                        _ParametersPoles.Add(VMind.Global.DBHelper.CreateIntParameter("@ClassFk", ParameterDirection.Input, true, DBNull.Value));
                    #endregion

                    #region AID
                    if (!string.IsNullOrEmpty(p.AID) )
                        _ParametersPoles.Add(VMind.Global.DBHelper.CreateNVarCharParameter("@AID", 100, ParameterDirection.Input, false, p.AID));
                    else
                        _ParametersPoles.Add(VMind.Global.DBHelper.CreateNVarCharParameter("@AID", 100, ParameterDirection.Input, true, DBNull.Value));
                    #endregion

                    #region LVisit
                    if (p.LVisit != Int32.MinValue)
                        _ParametersPoles.Add(VMind.Global.DBHelper.CreateIntParameter("@LVisit", ParameterDirection.Input, false, p.LVisit));
                    else
                        _ParametersPoles.Add(VMind.Global.DBHelper.CreateIntParameter("@LVisit", ParameterDirection.Input, true, DBNull.Value));
                    #endregion

                    #region Number
                    if (!string.IsNullOrEmpty(p.Number))
                        _ParametersPoles.Add(VMind.Global.DBHelper.CreateNVarCharParameter("@Number", 100, ParameterDirection.Input, false, p.Number));
                    else
                        _ParametersPoles.Add(VMind.Global.DBHelper.CreateNVarCharParameter("@Number", 100, ParameterDirection.Input, true, DBNull.Value));
                    #endregion

                    #region Length
                    if (p.Length != double.MinValue)
                        _ParametersPoles.Add(VMind.Global.DBHelper.CreateIntParameter("@Length", ParameterDirection.Input, false, p.Length));
                    else
                        _ParametersPoles.Add(VMind.Global.DBHelper.CreateIntParameter("@Length", ParameterDirection.Input, true, DBNull.Value));
                    #endregion

                    #region LVisitor
                    if (!string.IsNullOrEmpty(p.LVisitor))
                        _ParametersPoles.Add(VMind.Global.DBHelper.CreateNVarCharParameter("@LVIsitor", 100, ParameterDirection.Input, false, p.LVisitor));
                    else
                        _ParametersPoles.Add(VMind.Global.DBHelper.CreateNVarCharParameter("@LVIsitor", 100, ParameterDirection.Input, true, DBNull.Value));
                    #endregion


                    #endregion
                    _MobileQueryPoles.Parameters.Add(_ParametersPoles);
                }

                foreach (EBaseAssetImage i in v.Images)
                {
                    _ParametersImages = new List<SqlParameter>();
                    
                    #region Parameters
                    _ParametersImages.Add(VMind.Global.DBHelper.CreateIntParameter("@ImageId", ParameterDirection.Input, false, i.Id));
                    _ParametersImages.Add(VMind.Global.DBHelper.CreateIntParameter("@VisitFk", ParameterDirection.Input, false, i.Asset.Id));
                    _ParametersImages.Add(VMind.Global.DBHelper.CreateBitParameter("@SolutionAvailable", ParameterDirection.Input, false, i.SolutionAvailable));
                    
                    #region Pole

                    if(i.Pole.Id != Int32.MinValue)
                        _ParametersImages.Add(VMind.Global.DBHelper.CreateIntParameter("@PoleFk", ParameterDirection.Input, false, i.Pole.Id));
                    else
                        _ParametersImages.Add(VMind.Global.DBHelper.CreateIntParameter("@PoleFk", ParameterDirection.Input, true, DBNull.Value));



                    #endregion

                    _ParametersImages.Add(VMind.Global.DBHelper.CreateNVarCharParameter("@FileName", 255, ParameterDirection.Input, false, i.FileName));

                    #endregion

                    _MobileQueryImages.Parameters.Add(_ParametersImages);
                }
            }

            listQueries.Add(_MobileQueryStructures);
            listQueries.Add(_MobileQueryPoles);
            listQueries.Add(_MobileQueryImages);
            return listQueries;
        }

        public static DataSet GetVisitById(Int32 id,string connectionKey)
        {
            return DVendor_Visit_XMissionStructure.GetServerVisitsByID(id, connectionKey);


        }
    }
}
